<html>
<head>
<title>firstworks   Programming with SQL Relay using the C++ API</title>
<link href="../css/styles.css" rel="stylesheet">
</head>
<body>

<span class="heading1">Programming with SQL Relay using the C++ API</span><br><br>

<ul>
<li><a href="#compiling">Compiling an SQL Relay Client Program</a></li>
<li><a href="#session">Establishing a Sessions</a></li>
<li><a href="#query">Executing Queries</a></li>
<li><a href="#commit">Commits and Rollbacks</a></li>
<li><a href="#temptables">Temporary Tables</a></li>
<li><a href="#errors">Catching Errors</a></li>
<li><a href="#bindvars">Substitution and Bind Variables</a></li>
<li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
<li><a href="#fields">Accessing Fields in the Result Set</a></li>
<li><a href="#largeresultsets">Dealing With Large Result Sets</a></li>
<li><a href="#cursors">Cursors</a></li>
<li><a href="#columns">Getting Column Information</a></li>
<li><a href="#storedprocedures">Stored Procedures</a></li>
<li><a href="#caching">Caching The Result Set</a></li>
<li><a href="#suspending">Suspending and Resuming Sessions</a></li>
<li><a href="#lastinsertid">Getting the Last Insert ID in SQLite</a></li>
</ul>

<a name="compiling"></a>
<span class="heading2">Compiling an SQL Relay Client Program</span><br><br>

<p>When writing an SQL Relay client program using the C++ API, you need to
include the sqlrclient.h file.<p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
</PRE>
</blockquote>

<p>You'll also need to link against the sqlrclient and rudiments libraries.
The include file is usually found in /usr/local/firstworks/include and the
libraries are usually found in /usr/local/firstworks/lib.</p>

<p>The command to compile your .C file to object code will look something like
this (assuming you're using GNU C++):</p>

<blockquote>
g++ -I/usr/local/firstworks/include -c myprogram.C
</blockquote>

<p>The command to compile your .o file to an executable will look something like
this (assuming you're using GNU C++):</p>

<blockquote>
g++ -o myprogram myprogram.o -L/usr/local/firstworks/lib -lsqlrclient -lrudiments
</blockquote>


<a name="session"></a>
<span class="heading2">Establishing a Session</span><br><br>

<p>To use SQL Relay, you have to identify the connection that you intend to 
use.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

       sqlrconnection      con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);

       ... execute some queries ...

       <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>After calling the constructor, a session is established when the first 
query, ping() or identify() is run.</p>

<p>For the duration of the session, the client stays connected to a database
connection daemon.  While one client is connected, no other client can connect.
Care should be taken to minimize the length of a session.</p>

<p>If you're using a transactional database, ending a session has a catch.
Database connection daemons can be configured to send either a commit or
rollback at the end of a session if DML queries were executed during the
session with no commit or rollback.  Program accordingly.</p>

<a name="query"></a>
<span class="heading2">Executing Queries</span><br><br>

<p>Allocate a cursor, then call sendQuery() or sendFileQuery() to run a 
query.  The same cursor may be used over and over.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        sqlrconnection      *con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;sendQuery(<FONT color=#f800f8>&quot;select * from my_table&quot;</FONT>);

        ... do some stuff that takes a short time ...

        cur-&gt;sendFileQuery(<FONT color=#f800f8>&quot;/usr/local/myprogram/sql&quot;</FONT>,<FONT color=#f800f8>&quot;myquery.sql&quot;</FONT>);
        con-&gt;endSession();

        ... do some stuff that takes a long time ...

        cur-&gt;sendQuery(<FONT color=#f800f8>&quot;select * from my_other_table&quot;</FONT>);
        con-&gt;endSession();

        ... process the result set ...

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>Note the call to endSession() after the call to sendFileQuery().  Since the
program does some stuff that takes a long time between that query and the next,
ending the session there allows another client an opportunity to use that
database connection while your client is busy.  The next call to sendQuery() 
establishes another session.  Since the program does some stuff that takes a 
short time between the first two queries, it's OK to leave the session open 
between them.</p>

<a name="commit"></a>
<span class="heading2">Commits and Rollbacks</span><br><br>

<p>If you need to execute a commit or rollback, you should use the commit()
and rollback() methods of the sqlrconnection class rather than sending a 
"commit" or "rollback" query.  There are two reasons for this.  First, it's 
much more efficient to call the methods.  Second, if you're writing code that 
can run on transactional or non-transactional databases, some non-transactional 
databases will throw errors if they receive a "commit" or "rollback" query, but
by calling the commit() and rollback() methods you instruct the database 
connection daemon to call the commit and rollback API methods for that database 
rather than issuing them as queries.  If the API's have no commit or rollback 
methods, the calls do nothing and the database throws no error.  This is 
especially important when using SQL Relay with ODBC.</p>

<p>You can also turn Autocommit on or off with the autoCommitOn() and
autoCommitOff() methods of the sqlrconnection class.  When Autocommit is on,
the database performs a commit after each successful DML or DDL query.  When 
Autocommit is off, the database commits when the client instructs it to, or 
(by default) when a client disconnects.  For databases that don't support 
Autocommit, autoCommitOn() and autoCommitOff() have no effect.</p>


<a name="temptables"></a>
<span class="heading2">Temporary Tables</span><br><br>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"></a>
<span class="heading2">Catching Errors</span><br><br>

<p>If your call to sendQuery() or sendFileQuery() returns a 0, the query failed.
You can find out why by calling errorMessage().</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        <B><FONT color=#a02828>if</FONT></B> (!cur-&gt;sendQuery(<FONT color=#f800f8>&quot;select * from my_nonexistant_table&quot;</FONT>)) {
                cout &lt;&lt; cur-&gt;errorMessage() &lt;&lt; endl;
        }

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}

</PRE>
</blockquote>

<a name="bindvars"></a>
<span class="heading2">Substitution and Bind Variables</span><br><br>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The SQL Relay API provides methods for
making substitutions and binds in those queries.</p>

<p>For a detailed discussion of substitutions and binds, see
<a href="binds.html">this document</a>.</p>

<p>Rather than just calling sendQuery() you call prepareQuery(),
substitution(), inputBind() and executeQuery().  If you using queries
stored in a file, you can call prepareFileQuery() instead of prepareQuery().</p>

<p>When passing a floating point number in as a bind or substitution variable,
you have to supply precision and scale for the number.  See
<a href="precisionscale.html">this page</a> for a discussion of precision and
scale.</p>

<blockquote>
<pre>
<font color="#a020f0">#include </font><font color="#ff00ff">&lt;sqlrelay/sqlrclient.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a52a2a"><b>new</b></font> sqlrconnection(<font color="#ff00ff">&quot;host&quot;</font>,<font color="#ff00ff">9000</font>,<font color="#ff00ff">&quot;&quot;</font>,<font color="#ff00ff">&quot;user&quot;</font>,<font color="#ff00ff">&quot;password&quot;</font>,<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
        sqlrcursor          *cur=<font color="#a52a2a"><b>new</b></font> sqlrcursor(con);

        cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select * from mytable $(whereclause)&quot;</font>)
        cur-&gt;substitution(<font color="#ff00ff">&quot;whereclause&quot;</font>,<font color="#ff00ff">&quot;where stringcol=:stringval and integercol&gt;:integerval and floatcol&gt;floatval&quot;</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;stringval&quot;</font>,<font color="#ff00ff">&quot;true&quot;</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;integerval&quot;</font>,<font color="#ff00ff">10</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;floatval&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
        cur-&gt;executeQuery();

        ... process the result set ...

        <font color="#a52a2a"><b>delete</b></font> cur;
        <font color="#a52a2a"><b>delete</b></font> con;
}
</pre>
</blockquote>

<p>If you are curious how many bind variables have been declared in a query,
you can call countBindVariables() after preparing the query.</p>

<p>If you're using a database with an embedded procedural language, you may
want to retrieve data from function calls.  To facilitate this, SQL Relay
provides methods for defining and retrieving output bind variables.</p>

<blockquote>
<pre>
<font color="#a020f0">#include </font><font color="#ff00ff">&lt;sqlrelay/sqlrclient.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a52a2a"><b>new</b></font> sqlrconnection(<font color="#ff00ff">&quot;host&quot;</font>,<font color="#ff00ff">9000</font>,<font color="#ff00ff">&quot;&quot;</font>,<font color="#ff00ff">&quot;user&quot;</font>,<font color="#ff00ff">&quot;password&quot;</font>,<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
        sqlrcursor          *cur=<font color="#a52a2a"><b>new</b></font> sqlrcursor(con);

        cur-&gt;prepareQuery(<font color="#ff00ff">&quot;begin  :result1:=addTwoIntegers(:integer1,:integer2);  :result2=addTwoFloats(:float1,:float2);  :result3=convertToString(:integer3); end;&quot;</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;integer1&quot;</font>,<font color="#ff00ff">10</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;integer2&quot;</font>,<font color="#ff00ff">20</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;float1&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;float2&quot;</font>,<font color="#ff00ff">2.2</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
        cur-&gt;inputBind(<font color="#ff00ff">&quot;integer3&quot;</font>,<font color="#ff00ff">30</font>);
        cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;result1&quot;</font>);
        cur-&gt;defineOutputBindDouble(<font color="#ff00ff">&quot;result2&quot;</font>);
        cur-&gt;defineOutputBindString(<font color="#ff00ff">&quot;result3&quot;</font>,<font color="#ff00ff">100</font>);
        cur-&gt;executeQuery();
        <font color="#2e8b57"><b>int64_t</b></font>    result1=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;result1&quot;</font>);
        <font color="#2e8b57"><b>double</b></font>     result2=cur-&gt;getOutputBindDouble(<font color="#ff00ff">&quot;result2&quot;</font>);
        <font color="#2e8b57"><b>char</b></font>       *result3=cur-&gt;getOutputBindString(<font color="#ff00ff">&quot;result3&quot;</font>);
        con-&gt;endSession();

        ... <font color="#a52a2a"><b>do</b></font> something with the result ...

        <font color="#a52a2a"><b>delete</b></font> cur;
        <font color="#a52a2a"><b>delete</b></font> con;
}
</pre>
</blockquote>

<p>The getOutputBindString() method returns a NULL value as an empty string.  
If you would it to come back as a NULL instead, you can call the 
getNullsAsNulls() method.  To revert to the default behavior, you can
call getNullsAsEmptyStrings().</p>

<p>If you are using Oracle 8i or higher, you can insert data into BLOB
and CLOB columns using the inputBindBlob(), inputBindClob() methods.</p>

<blockquote>
<pre>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;sqlrelay/sqlrclient.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a02828"><b>new</b></font> sqlrconnection(<font color="#f800f8">&quot;host&quot;</font>,<font color="#f800f8">9000</font>,<font color="#f800f8">&quot;&quot;</font>,<font color="#f800f8">&quot;user&quot;</font>,<font color="#f800f8">&quot;password&quot;</font>,<font color="#f800f8">0</font>,<font color="#f800f8">1</font>);
        sqlrcursor          *cur=<font color="#a02828"><b>new</b></font> sqlrcursor(con);

        cur-&gt;executeQuery(<font color="#f800f8">&quot;create table images (image blob, description clob)&quot;</font>);

        <font color="#288850"><b>unsigned</b></font> <font color="#288850"><b>char</b></font>   imagedata[<font color="#f800f8">40000</font>];
        <font color="#288850"><b>unsigned</b></font> <font color="#288850"><b>long</b></font>   imagelength;

        ... read an image from a file into imagedata and the length of the
                file into imagelength ...

        <font color="#288850"><b>unsigned</b></font> <font color="#288850"><b>char</b></font>   description[<font color="#f800f8">40000</font>];
        <font color="#288850"><b>unsigned</b></font> <font color="#288850"><b>long</b></font>   desclength;

        ... read a description from a file into description and the length of
                the file into desclength ...

        cur-&gt;prepareQuery(<font color="#f800f8">&quot;insert into images values (:image,:desc)&quot;</font>);
        cur-&gt;inputBindBlob(<font color="#f800f8">&quot;image&quot;</font>,imagedata,imagelength);
        cur-&gt;inputBindClob(<font color="#f800f8">&quot;desc&quot;</font>,description,desclength);
        cur-&gt;executeQuery();

        <font color="#a02828"><b>delete</b></font> cur;
        <font color="#a02828"><b>delete</b></font> con;
}
</pre>
</blockquote>

<p>Likewise, with Oracle 8i, you can retreive BLOB or CLOB data using
defineOutputBindBlob()/getOutputBindBlob() and
defineOutputBindClob()/getOutputBindClob().</p>

<blockquote>
<pre>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;sqlrelay/sqlrclient.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a02828"><b>new</b></font> sqlrconnection(<font color="#f800f8">&quot;host&quot;</font>,<font color="#f800f8">9000</font>,<font color="#f800f8">&quot;&quot;</font>,<font color="#f800f8">&quot;user&quot;</font>,<font color="#f800f8">&quot;password&quot;</font>,<font color="#f800f8">0</font>,<font color="#f800f8">1</font>);
        sqlrcursor          *cur=<font color="#a02828"><b>new</b></font> sqlrcursor(con);

        cur-&gt;prepareQuery(<font color="#f800f8">&quot;begin  select image into :image from images;  select description into :desc from images;  end;&quot;</font>);
        cur-&gt;defineOutputBindBlob(<font color="#f800f8">&quot;image&quot;</font>);
        cur-&gt;defineOutputBindClob(<font color="#f800f8">&quot;desc&quot;</font>);
        cur-&gt;executeQuery();

        <font color="#288850"><b>char</b></font>    *image=cur-&gt;getOutputBindBlob(<font color="#f800f8">&quot;image&quot;</font>);
        <font color="#288850"><b>long</b></font>    imagelength=cur-&gt;getOutputBindLength(<font color="#f800f8">&quot;image&quot;</font>);

        <font color="#288850"><b>char</b></font>    *desc=cur-&gt;getOutputBindClob(<font color="#f800f8">&quot;desc&quot;</font>);
        <font color="#288850"><b>char</b></font>    *desclength=cur-&gt;getOutputBindLength(<font color="#f800f8">&quot;desc&quot;</font>);

        con-&gt;endSession();

        ... do something with image and desc ...

        <font color="#a02828"><b>delete</b></font> cur;
        <font color="#a02828"><b>delete</b></font> con;
}
</pre>
</blockquote>

<p>Sometimes its convenient to bind a bunch of variables that may or may not 
actually be in the query.  For example, if you are building a web based
application, it may be easy to just bind all the form variables/values from the
previous page, even though some of them don't appear in the query.  Databases
usually generate errors in this case.  Calling validateBinds() just prior to
calling executeQuery() causes the API to check the query for each bind variable
before actually binding it, preventing those kinds of errors.   You can also
call validBind() to see if a specific variable is valid.  However there is
a performance cost associated with calling validateBinds() and validBind().</p>

<a name="rebinding"></a>
<span class="heading2">Re-Binding and Re-Execution</span><br><br>

<p>Another feature of the prepare/bind/execute paradigm is the ability to 
prepare, bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        sqlrconnection      *con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;prepareQuery(<FONT color=#f800f8>&quot;select * from mytable where mycolumn&gt;:value&quot;</FONT>);
        cur-&gt;inputBind(<FONT color=#f800f8>&quot;value&quot;</FONT>,<FONT color=#f800f8>1</FONT>);
        cur-&gt;executeQuery();

        ... process the result set ...

        cur-&gt;clearBinds();
        cur-&gt;inputBind(<FONT color=#f800f8>&quot;value&quot;</FONT>,<FONT color=#f800f8>5</FONT>);
        cur-&gt;executeQuery();

        ... process the result set ...

        cur-&gt;clearBinds();
        cur-&gt;inputBind(<FONT color=#f800f8>&quot;value&quot;</FONT>,<FONT color=#f800f8>10</FONT>);
        cur-&gt;executeQuery();

        ... process the result set ...

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<a name="fields"></a>
<span class="heading2">Accessing Fields in the Result Set</span><br><br>

<p>The rowCount(), colCount() and getField() methods are useful for processing
result sets.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;sendQuery(<FONT color=#f800f8>&quot;select * from my_table&quot;</FONT>);
        con-&gt;endSession();

        <B><FONT color=#a02828>for</FONT></B> (<B><FONT color=#288850>int</FONT></B> row=<FONT color=#f800f8>0</FONT>; row&lt;cur-&gt;rowCount(); row++) {
                <B><FONT color=#a02828>for</FONT></B> (<B><FONT color=#288850>int</FONT></B> col=<FONT color=#f800f8>0</FONT>; col&lt;cur-&gt;colCount(); col++) {
                        cout &lt;&lt; cur-&gt;getField(row,col) &lt;&lt; <FONT color=#f800f8>&quot;,&quot;</FONT>;
                }
                cout &lt;&lt; endl;
        }

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>The getField() method returns a string.  If you would like to get a field as
a long or double, you can use getFieldAsLong() and getFieldAsDouble().</p>

<p>You can also use getRow() which returns a NULL-terminated array of the
fields in the row.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_table&quot;</FONT>);
        con-&gt;endSession();

        <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> row=<FONT color=#ff00ff>0</FONT>; row&lt;cur-&gt;rowCount(); row++) {
                <B><FONT color=#2e8b57>char</FONT></B>    **rowarray=cur-&gt;getRow(row);
                <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> col=<FONT color=#ff00ff>0</FONT>; col&lt;cur-&gt;colCount(); col++) {
                        cout &lt;&lt; rowarray[col] &lt;&lt; <FONT color=#ff00ff>&quot;,&quot;</FONT>;
                }
                cout &lt;&lt; endl;
        }

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>The getField() and getRow() methods return NULL fields as empty strings.
If you would like them to come back as NULL's instead, you can call the
getNullsAsNulls() method.  To revert to the default behavior, you can call
getNullsAsEmptyStrings().</p>

<p>If you want to access the result set, but don't care about the
column information (column names, types or sizes) and don't mind getting
fields by their numeric index instead of by name,  you can call the
dontGetColumnInfo() method prior to executing your query.  This can result
in a performance improvement, especially when many queries with small
result sets are executed in rapid succession.  You can call getColumnInfo()
again later to turn off this feature.</p>

<a name="largeresultsets"></a>
<span class="heading2">Dealing With Large Result Sets</span><br><br>

<p>SQL Relay normally buffers the entire result set.  This can speed things up 
at the cost of memory.  With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.</p>

<p>Use setResultSetBufferSize() to set the number of rows to buffer at a time.
Calls to getRow() and getField() cause the chunk containing the requested field
to be fetched.  Rows in that chunk are accessible but rows before it are 
not.</p>

<p>For example, if you setResultSetBufferSize(5) and execute a query that 
returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, 
then 15-19.  When rows 5-9 are available, getField(0,0) will return NULL and 
getField(11,0) will cause rows 10-14 to be fetched and return the requested
value.</p>

<p>When buffering the result set in chunks, don't end the session until after
you're done with the result set.</p>

<p>If you call setResultSetBufferSize() and forget what you set it to, you
can always call getResultSetBufferSize().</p>

<p>When buffering a result set in chunks, the rowCount() method returns
the number of rows returned so far.  The firstRowIndex() method returns the
index of the first row of the currently buffered chunk.

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;setResultSetBufferSize(<FONT color=#ff00ff>5</FONT>);

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_table&quot;</FONT>);

        <B><FONT color=#2e8b57>int</FONT></B>     done=<FONT color=#ff00ff>0</FONT>;
        <B><FONT color=#2e8b57>int</FONT></B>     row=<FONT color=#ff00ff>0</FONT>;
        <B><FONT color=#2e8b57>char</FONT></B>    *field;
        <B><FONT color=#a52a2a>while</FONT></B> (!done) {
                <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> col=<FONT color=#ff00ff>0</FONT>; col&lt;cur-&gt;colCount(); col++) {
                        <B><FONT color=#a52a2a>if</FONT></B> (field=cur-&gt;getField(row,col)) {
                                cout &lt;&lt; field &lt;&lt; <FONT color=#ff00ff>&quot;,&quot;</FONT>;
                        } <B><FONT color=#a52a2a>else</FONT></B> {
                                done=<FONT color=#ff00ff>1</FONT>;
                        }
                }
                cout &lt;&lt; endl;
                row++;
        }

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_other_table&quot;</FONT>);

        ... process this query's result set in chunks also ...

        cur-&gt;setResultSetBufferSize(<FONT color=#ff00ff>0</FONT>);

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_third_table&quot;</FONT>);

        ... process this query's result set all at once ...

        con-&gt;endSession();

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<a name="cursors"></a>
<span class="heading2">Cursors</span><br><br>

<p>Cursors make it possible to execute queries while processing the result
set of another query.  You can select rows from a table in one query, then 
iterate through its result set, inserting rows into another table, using only 
1 database connection for both operations.</p>

<p>For example:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#f800f8>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        sqlrconnection      *con=<B><FONT color=#a02828>new</FONT></B> sqlrconnection(<FONT color=#f800f8>&quot;host&quot;</FONT>,<FONT color=#f800f8>9000</FONT>,<FONT color=#f800f8>&quot;&quot;</FONT>,<FONT color=#f800f8>&quot;user&quot;</FONT>,<FONT color=#f800f8>&quot;password&quot;</FONT>,<FONT color=#f800f8>0</FONT>,<FONT color=#f800f8>1</FONT>);
        sqlrcursor          *cursor1=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);
        sqlrcursor          *cursor2=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cursor1-&gt;setResultSetBufferSize(<FONT color=#f800f8>10</FONT>);
        cursor1-&gt;sendQuery(<FONT color=#f800f8>&quot;select * from my_huge_table&quot;</FONT>);

        <B><FONT color=#288850>int</FONT></B>     index=<FONT color=#f800f8>0</FONT>;
        <B><FONT color=#a02828>while</FONT></B> (!cursor1-&gt;endOfResultSet()) {
                cursor2-&gt;prepareQuery(<FONT color=#f800f8>&quot;insert into my_other_table values (:1,:2,:3)&quot;</FONT>);
                cursor2-&gt;inputBind(<FONT color=#f800f8>&quot;1&quot;</FONT>,cursor1-&gt;getField(index,<FONT color=#f800f8>1</FONT>));
                cursor2-&gt;inputBind(<FONT color=#f800f8>&quot;2&quot;</FONT>,cursor1-&gt;getField(index,<FONT color=#f800f8>2</FONT>));
                cursor2-&gt;inputBind(<FONT color=#f800f8>&quot;3&quot;</FONT>,cursor1-&gt;getField(index,<FONT color=#f800f8>3</FONT>));
                cursor2-&gt;executeQuery();
        }

        <B><FONT color=#a02828>delete</FONT></B> cursor2;
        <B><FONT color=#a02828>delete</FONT></B> cursor1;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>Prior to SQL Relay version 0.25, you would have had to buffer the first
result set or use 2 database connections instead of just 1.</p>

<p>If you are using stored procedures with Oracle 8i or higher, a stored
procedure can execute a query and return a cursor.  A cursor bind variable can
then retrieve that cursor.  Your program can retrieve the result set from the
cursor.  All of this can be accomplished using defineOutputBindCursor(),
getOutputBindCursor() and fetchFromOutputBindCursor().</p>

<blockquote>
PL/SQL Procedure:<br>
<pre>
<font color="#6858c8">FUNCTION</font> sp_mytable <font color="#6858c8">RETURN</font> types.cursorType
l_cursor types.cursorType;
<font color="#6858c8">BEGIN</font>
        OPEN l_cursor <font color="#6858c8">FOR</font> <font color="#a02828"><b>SELECT</b></font> * <font color="#6858c8">FROM</font> mytable;
        <font color="#6858c8">RETURN</font> l_cursor;
<font color="#6858c8">END</font>;
</pre>
<br>
Program code:
<pre>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;sqlrelay/sqlrclient.h&gt;</font>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a02828"><b>new</b></font> sqlrconnection(<font color="#f800f8">&quot;host&quot;</font>,<font color="#f800f8">9000</font>,<font color="#f800f8">&quot;&quot;</font>,<font color="#f800f8">&quot;user&quot;</font>,<font color="#f800f8">&quot;password&quot;</font>,<font color="#f800f8">0</font>,<font color="#f800f8">1</font>);
        sqlrcursor          *cur=<font color="#a02828"><b>new</b></font> sqlrcursor(con);

        cur-&gt;prepareQuery(<font color="#f800f8">&quot;begin  :curs:=sp_mytable; end;&quot;</font>);
        cur-&gt;defineOutputBindCursor(<font color="#f800f8">&quot;curs&quot;</font>);
        cur-&gt;executeQuery();

        sqlrcursor      *bindcur=cur-&gt;getOutputBindCursor(<font color="#f800f8">&quot;curs&quot;</font>);
        bindcur-&gt;fetchFromBindCursor();

        <font color="#0000f8">// print fields from table</font>
        <font color="#a02828"><b>for</b></font> (<font color="#288850"><b>int</b></font> i=<font color="#f800f8">0</font>; i&lt;bindcur-&gt;rowCount(); i++) {
                <font color="#a02828"><b>for</b></font> (<font color="#288850"><b>int</b></font> j=<font color="#f800f8">0</font>; j&lt;bindcur-&gt;colCount(); j++) {
                        cout &lt;&lt; bindcur-&gt;getField(i,j) &lt;&lt; <font color="#f800f8">&quot;, &quot;</font>;
                }
                cout &lt;&lt; endl;
        }

        <font color="#a02828"><b>delete</b></font> bindcur;

        <font color="#a02828"><b>delete</b></font> cur;
        <font color="#a02828"><b>delete</b></font> con;
}
</pre>
</blockquote>

<p>The number of cursors simultaneously available per-connection is set at 
compile time and defaults to 5.</p>

<a name="columns"></a>
<span class="heading2">Getting Column Information</span><br><br>

<p>For each column, the API supports getting the name, type and length of each
field.  All databases support these attributes.  The API also supports
getting the precision, scale (see <a href="precisionscale.html">this page</a>
for a discussion of precision and scale), length of the longest field, and
whether the
column is nullable, the primary key, unique, part of a key, unsigned,
zero-filled, binary, or an auto-incrementing field.  However, not all databases
support these attributes.  If a database doesn't support an attribute, it is
always returned as false.</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_table&quot;</FONT>);
        con-&gt;endSession();

        <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> i=<FONT color=#ff00ff>0</FONT>; i&lt;cur-&gt;colCount(); i++) {
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Name:          &quot;</FONT> &lt;&lt; cur-&gt;getColumnName(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Type:          &quot;</FONT> &lt;&lt; cur-&gt;getColumnType(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Length:        &quot;</FONT> &lt;&lt; cur-&gt;getColumnLength(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Precision:     &quot;</FONT> &lt;&lt; cur-&gt;getColumnPrecision(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Scale:         &quot;</FONT> &lt;&lt; cur-&gt;getColumnScale(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Longest Field: &quot;</FONT> &lt;&lt; cur-&gt;getLongest(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Nullable:      &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsNullable(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Primary Key:   &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsPrimaryKey(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Unique:        &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsUnique(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Part of Key:   &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsPartOfKey(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Unsigned:      &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsUnsigned(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Zero Filled:   &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsZeroFilled(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Binary:        &quot;</FONT> &lt;&lt; cur-&gt;getColumnIsBinary(i) &lt;&lt; endl;
                cout &lt;&lt; <FONT color=#ff00ff>&quot;Auto Increment:&quot;</FONT> &lt;&lt; cur-&gt;getColumnIsAutoIncrement(i) &lt;&lt; endl;
                cout &lt;&lt; endl;
        }

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>Some databases force column names to upper case, others force column names
to lower case, and others still support mixed-case column names.  Sometimes,
when migrating between databases, you can run into trouble.  You can use
upperCaseColumnNames() and lowerCaseColumnNames() to cause column names to be
converted to upper or lower case, or you can use mixedCaseColumnNames() to
cause column names to be returned in the same case as they are defined in the
database.</p>

<blockquote>
<pre>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;sqlrelay/sqlrclient.h&gt;</font>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        sqlrconnection      *con=<font color="#a02828"><b>new</b></font> sqlrconnection(<font color="#f800f8">&quot;host&quot;</font>,<font color="#f800f8">9000</font>,<font color="#f800f8">&quot;&quot;</font>,<font color="#f800f8">&quot;user&quot;</font>,<font color="#f800f8">&quot;password&quot;</font>,<font color="#f800f8">0</font>,<font color="#f800f8">1</font>);
        sqlrcursor          *cur=<font color="#a02828"><b>new</b></font> sqlrcursor(con);

        <font color="#0000f8">// column names will be forced to upper case</font>
        cur-&gt;upperCaseColumnNames();
        cur-&gt;sendQuery(<font color="#f800f8">&quot;select * from my_table&quot;</font>);
        con-&gt;endSession();

        <font color="#a02828"><b>for</b></font> (<font color="#288850"><b>int</b></font> i=<font color="#f800f8">0</font>; i&lt;cur-&gt;colCount(); i++) {
                cout &lt;&lt; <font color="#f800f8">&quot;Name:          &quot;</font> &lt;&lt; cur-&gt;getColumnName(i) &lt;&lt; endl;
                cout &lt;&lt; endl;
        }

        <font color="#0000f8">// column names will be forced to lower case</font>
        cur-&gt;lowerCaseColumnNames();
        cur-&gt;sendQuery(<font color="#f800f8">&quot;select * from my_table&quot;</font>);
        con-&gt;endSession();

        <font color="#a02828"><b>for</b></font> (<font color="#288850"><b>int</b></font> i=<font color="#f800f8">0</font>; i&lt;cur-&gt;colCount(); i++) {
                cout &lt;&lt; <font color="#f800f8">&quot;Name:          &quot;</font> &lt;&lt; cur-&gt;getColumnName(i) &lt;&lt; endl;
                cout &lt;&lt; endl;
        }

        <font color="#0000f8">// column names will be the same as they are in the database</font>
        cur-&gt;mixedCaseColumnNames();
        cur-&gt;sendQuery(<font color="#f800f8">&quot;select * from my_table&quot;</font>);
        con-&gt;endSession();

        <font color="#a02828"><b>for</b></font> (<font color="#288850"><b>int</b></font> i=<font color="#f800f8">0</font>; i&lt;cur-&gt;colCount(); i++) {
                cout &lt;&lt; <font color="#f800f8">&quot;Name:          &quot;</font> &lt;&lt; cur-&gt;getColumnName(i) &lt;&lt; endl;
                cout &lt;&lt; endl;
        }

        <font color="#a02828"><b>delete</b></font> cur;
        <font color="#a02828"><b>delete</b></font> con;
}
</pre>
</blockquote>

<a name="storedprocedures"></a>
<span class="heading2">Stored Procedures</span><br><br>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>SQL Relay supports stored procedures for most databases, but there are
some caveats.  Stored procedures are not currently supported when using FreeTDS
against Sybase or Microsoft SQL Server.  Blob/Clob bind variables are only
supported in Oracle 8i or higher.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<span class="heading3">No Values</span><br>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;begin testproc(:in1,:in2,:in3); end;&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc @in1 int, @in2 <font color="#298a52"><b>float</b></font>, @in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>) <font color="#6b59ce">as</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (@in1,@in2,@in3)
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;exec testproc&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;execute procedure testproc ?, ?, ?&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">DB2</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(<font color="#a52829"><b>in</b></font> in1 int, <font color="#a52829"><b>in</b></font> in2 double, <font color="#a52829"><b>in</b></font> in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) language sql
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> (in1,in2,in3);
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;call testproc(?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns void <font color="#6b59ce">as</font> '
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>insert</b></font> <font color="#6b59ce">into</font> mytable <font color="#6b59ce">values</font> ($<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font>);
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select testfunc(:in1,:in2,:in3)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
<font color="#6a5acd">begin</font>
        <font color="#a52a2a"><b>insert</b></font> <font color="#6a5acd">into</font> mytable <font color="#6a5acd">values</font> (in1,in2,in3);
<font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;call testproc(:in1,:in2,:in3)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>



<br><span class="heading3">Single Values</span><br>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.</p>

<p>Here is an example where the procedure itself returns a value.  Note that
Oracle calls these functions.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>) returns <font color="#298a52"><b>number</b></font> <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">return</font> in1;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select testproc(:in1,:in2,:in3) from dual&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>char</b></font>    *result=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testproc
</pre>
</blockquote>

<p>Here is an example where the value is returned through an output
parameter.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>, out1 <font color="#a52829"><b>out</b></font> <font color="#298a52"><b>number</b></font>) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1:=in1;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;begin testproc(:in1,:in2,:in3,:out1); end;&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font>    result=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc @in1 int, @in2 <font color="#298a52"><b>float</b></font>, @in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>), @out1 int output <font color="#6b59ce">as</font>
        <font color="#a52829"><b>select</b></font> @out1=@in1
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;exec testproc&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font>    result=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select * from testproc(?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>char</b></font>    *result=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>Alternatively, you can run a query like the following and receive the result
using an output bind variable.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
output bind variable is 1 rather than 4, even though there were 3 input bind
variables.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;execute procedure testproc ?, ?, ?&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;1&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font>    result=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;1&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(<font color="#a52829"><b>in</b></font> in1 int, <font color="#a52829"><b>in</b></font> in2 double, <font color="#a52829"><b>in</b></font> in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>), <font color="#a52829"><b>out</b></font> out1 int) language sql
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>set</b></font> out1 = in1;
<font color="#6b59ce">end</font>
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;call testproc(?,?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;4&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font>    result=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;4&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6b59ce">as</font> '
declare
        in1 int;
        in2 <font color="#298a52"><b>float</b></font>;
        in3 <font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>);
<font color="#6b59ce">begin</font>
        in1:=$<font color="#ff00ff">1</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select * from testfunc(:in1,:in2,:in3)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">4</font>,<font color="#ff00ff">2</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>char</b></font>    *result=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>A single value can be returned from a MySQL function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">function</font> testfunc(in1 int, in2 <font color="#2e8b57"><b>float</b></font>, in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns int <font color="#6a5acd">return</font> in1;
</pre>
</blockquote>

<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select testfunc(?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">4</font>,<font color="#ff00ff">2</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#2e8b57"><b>char</b></font>    *result=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>To drop the function, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">function</font> testfunc
</pre>
</blockquote>

<p>A single value can be returned in the result set of a MySQL procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;select testproc()&quot;</font>);
<font color="#2e8b57"><b>char</b></font>    *result=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>A single value can be returned using the output variable of a MySQL
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font> <font color="#6a5acd">into</font> out1; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;set @out1=0&quot;</font>);
cur-&gt;sendQuery(<font color="#ff00ff">&quot;call testproc(@out1)&quot;</font>);
cur-&gt;sendQuery(<font color="#ff00ff">&quot;select @out1&quot;</font>);
<font color="#2e8b57"><b>char</b></font>    *result=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
</pre>
</blockquote>

<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>



<br><span class="heading3">Multiple Values</span><br>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.  If a procedure needs to return
multiple values, it can return one of them as the return value of the procedure
itself, but the rest must be returned through output parameters.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in2 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>number</b></font>, in3 <font color="#a52829"><b>in</b></font> <font color="#298a52"><b>varchar2</b></font>, out1 <font color="#a52829"><b>out</b></font> <font color="#298a52"><b>number</b></font>, out2 <font color="#a52829"><b>out</b></font> <font color="#298a52"><b>number</b></font>, out3 <font color="#a52829"><b>out</b></font> <font color="#298a52"><b>varchar2</b></font>) <font color="#6b59ce">is</font>
<font color="#6b59ce">begin</font>
        out1:=in1;
        out2:=in2;
        out3:=in3;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;begin testproc(:in1,:in2,:in3,:out1,:out2,:out3); end;&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
cur-&gt;defineOutputBindDouble(<font color="#ff00ff">&quot;out2&quot;</font>);
cur-&gt;defineOutputBindString(<font color="#ff00ff">&quot;out3&quot;</font>,<font color="#ff00ff">20</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font> out1=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
<font color="#298a52"><b>int64_t</b></font> out2=cur-&gt;getOutputBindDouble(<font color="#ff00ff">&quot;out2&quot;</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getOutputBindString(<font color="#ff00ff">&quot;out3&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc @in1 int, @in2 <font color="#298a52"><b>float</b></font>, @in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>), @out1 int output, @out2 int output, @out3 int output <font color="#6b59ce">as</font>
        <font color="#a52829"><b>select</b></font> @out1=@in1,
                @out2=@in2,
                @out3=@in3
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;exec testproc&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
cur-&gt;defineOutputBindDouble(<font color="#ff00ff">&quot;out2&quot;</font>);
cur-&gt;defineOutputBindString(<font color="#ff00ff">&quot;out3&quot;</font>,<font color="#ff00ff">20</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font> out1=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;out1&quot;</font>);
<font color="#298a52"><b>double</b></font>  out2=cur-&gt;getOutputBindDouble(<font color="#ff00ff">&quot;out2&quot;</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getOutputBindString(<font color="#ff00ff">&quot;out3&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Firebird</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(in1 <font color="#298a52"><b>integer</b></font>, in2 <font color="#298a52"><b>float</b></font>, in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) returns (out1 <font color="#298a52"><b>integer</b></font>, out2 <font color="#298a52"><b>float</b></font>, out3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6b59ce">as</font>
<font color="#6b59ce">begin</font>
        out1=in1;
        out2=in2;
        out3=in3;
        suspend;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select * from testproc(?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>char</b></font>    *out1=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *out2=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
</pre>
</blockquote>

<p>Alternatively, you can run a query like the following and receive the result
using a output bind variables.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
first output bind variable is 1 rather than 4, even though there were 3 input
bind variables.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;execute procedure testproc ?, ?, ?&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;1&quot;</font>);
cur-&gt;defineOutputBindDouble(<font color="#ff00ff">&quot;2&quot;</font>);
cur-&gt;defineOutputBindString(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">20</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font> out1=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;1&quot;</font>);
<font color="#298a52"><b>double</b></font>  out2=cur-&gt;getOutputBindDouble(<font color="#ff00ff">&quot;2&quot;</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getOutputBindString(<font color="#ff00ff">&quot;3&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">DB2</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">procedure</font> testproc(<font color="#a52829"><b>in</b></font> in1 int, <font color="#a52829"><b>in</b></font> in2 double, <font color="#a52829"><b>in</b></font> in3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>), <font color="#a52829"><b>out</b></font> out1 int, <font color="#a52829"><b>out</b></font> out2 double, <font color="#a52829"><b>out</b></font> out3 <font color="#298a52"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) language sql
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>set</b></font> out1 = in1;
        <font color="#a52829"><b>set</b></font> out2 = in2;
        <font color="#a52829"><b>set</b></font> out3 = in3;
<font color="#6b59ce">end</font>
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;call testproc(?,?,?,?,?,?)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">2</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;defineOutputBindInteger(<font color="#ff00ff">&quot;4&quot;</font>);
cur-&gt;defineOutputBindDouble(<font color="#ff00ff">&quot;5&quot;</font>);
cur-&gt;defineOutputBindString(<font color="#ff00ff">&quot;6&quot;</font>,<font color="#ff00ff">25</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>int64_t</b></font> out1=cur-&gt;getOutputBindInteger(<font color="#ff00ff">&quot;4&quot;</font>);
<font color="#298a52"><b>double</b></font>  out2=cur-&gt;getOutputBindDouble(<font color="#ff00ff">&quot;5&quot;</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getOutputBindString(<font color="#ff00ff">&quot;6&quot;</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>)) returns record <font color="#6b59ce">as</font> '
declare
        output record;
<font color="#6b59ce">begin</font>
        <font color="#a52829"><b>select</b></font> $<font color="#ff00ff">1</font>,$<font color="#ff00ff">2</font>,$<font color="#ff00ff">3</font> <font color="#6b59ce">into</font> output;
        <font color="#6b59ce">return</font> output;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;select * from testfunc(:in1,:in2,:in3) as (col1 int, col2 float, col3 char(20))&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">4</font>,<font color="#ff00ff">2</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#298a52"><b>char</b></font>    *out1=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *out2=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *out3=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc(int,<font color="#298a52"><b>float</b></font>,<font color="#298a52"><b>char</b></font>(<font color="#ff00ff">20</font>))
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>Here's how you can get multiple values from the result
set of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>in</b></font> in1 int, <font color="#a52a2a"><b>in</b></font> in2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>in</b></font> in3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>))
<font color="#6a5acd">begin</font>
	<font color="#a52a2a"><b>select</b></font> in1, in2, in3;
<font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the
Statement API cannot be used to call stored procedures yet.  As a result,
SQL Relay must fake input binds when calling MySQL stored procedures, so queries
which call stored procedures must be written with colon-delimited bind
variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;call testproc(:in1,:in2,:in3)&quot;</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in1&quot;</font>,<font color="#ff00ff">1</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in2&quot;</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">4</font>,<font color="#ff00ff">2</font>);
cur-&gt;inputBind(<font color="#ff00ff">&quot;in3&quot;</font>,<font color="#ff00ff">&quot;hello&quot;</font>);
cur-&gt;executeQuery();
<font color="#2e8b57"><b>char</b></font>    *out1=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *out2=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *out3=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>

<p>Here's how you can get multiple values from the output variables
of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc(<font color="#a52a2a"><b>out</b></font> out1 int, <font color="#a52a2a"><b>out</b></font> out2 <font color="#2e8b57"><b>float</b></font>, <font color="#a52a2a"><b>out</b></font> out3 <font color="#2e8b57"><b>varchar</b></font>(<font color="#ff00ff">20</font>)) <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> <font color="#ff00ff">1</font>,<font color="#ff00ff">1.1</font>,<font color="#ff00ff">'hello'</font> <font color="#6a5acd">into</font> out1, out2, out3; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;set @out1=0, @out2=0.0, @out3=''&quot;</font>);
cur-&gt;sendQuery(<font color="#ff00ff">&quot;call testproc(@out1,@out3,@out3)&quot;</font>);
cur-&gt;sendQuery(<font color="#ff00ff">&quot;select @out1,@out2,@out3&quot;</font>);
<font color="#2e8b57"><b>char</b></font>    *out1=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *out2=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *out3=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>



<br><span class="heading3">Result Sets</span><br>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<span class="heading4">Oracle</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#a52829"><b>or</b></font> replace package types <font color="#6b59ce">as</font>
        <font color="#6b59ce">type</font> cursorType <font color="#6b59ce">is</font> ref <font color="#6b59ce">cursor</font>;
<font color="#6b59ce">end</font>;

<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testproc <font color="#6b59ce">return</font> types.cursortype <font color="#6b59ce">is</font>
        l_cursor    types.cursorType;
<font color="#6b59ce">begin</font>
        open l_cursor <font color="#6b59ce">for</font> <font color="#a52829"><b>select</b></font> * <font color="#6b59ce">from</font> mytable;
        <font color="#6b59ce">return</font> l_cursor;
<font color="#6b59ce">end</font>;
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;prepareQuery(<font color="#ff00ff">&quot;begin  :curs:=testproc; end;&quot;</font>);
cur-&gt;defineOutputBindCursor(<font color="#ff00ff">&quot;curs&quot;</font>);
cur-&gt;executeQuery();
sqlrcursor      *bindcur=cur-&gt;getOutputBindCursor(<font color="#ff00ff">&quot;curs&quot;</font>);
bindcur-&gt;fetchFromBindCursor();
<font color="#298a52"><b>char</b></font>    *field00=bindcur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *field01=bindcur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *field02=bindcur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
<font color="#298a52"><b>char</b></font>    *field10=bindcur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *field11=bindcur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *field12=bindcur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">2</font>);
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testproc
<font color="#a52829"><b>drop</b></font> package types
</pre>
</blockquote>


<span class="heading4">Sybase and Microsoft SQL Server</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc <font color="#6a5acd">as</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> testtable
</pre>
</blockquote>

<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;exec testproc&quot;</font>);
<font color="#2e8b57"><b>char</b></font>    *field00=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *field01=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *field02=cur-&gt;getFieldByIndex(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
<font color="#2e8b57"><b>char</b></font>    *field10=cur-&gt;getFieldByIndex(<font color="#ff00ff">1</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *field11=cur-&gt;getFieldByIndex(<font color="#ff00ff">1</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *field12=cur-&gt;getFieldByIndex(<font color="#ff00ff">1</font>,<font color="#ff00ff">2</font>);
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>


<span class="heading4">Firebird</span><br>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<span class="heading4">DB2</span><br>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<span class="heading4">Postgresql</span><br>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>create</b></font> <font color="#6b59ce">function</font> testfunc() returns setof record <font color="#6b59ce">as</font> '
        declare output record;
<font color="#6b59ce">begin</font>
        <font color="#6b59ce">for</font> output <font color="#a52829"><b>in</b></font> <font color="#a52829"><b>select</b></font> * <font color="#6b59ce">from</font> mytable <font color="#6b59ce">loop</font>
                <font color="#6b59ce">return</font> next output;
        <font color="#6b59ce">end</font> <font color="#6b59ce">loop</font>;
        <font color="#6b59ce">return</font>;
<font color="#6b59ce">end</font>;
' language plpgsql
</pre>
</blockquote>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;select * from testfunc() as (testint int, testfloat float, testchar char(40))&quot;</font>);
<font color="#298a52"><b>char</b></font>    *field00=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *field01=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *field02=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
<font color="#298a52"><b>char</b></font>    *field10=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">0</font>);
<font color="#298a52"><b>char</b></font>    *field11=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">1</font>);
<font color="#298a52"><b>char</b></font>    *field12=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">2</font>);
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52829"><b>drop</b></font> <font color="#6b59ce">function</font> testfunc
</pre>
</blockquote>

<span class="heading4">MySQL</span><br>

<p>The result sets of all select statements called within MySQL stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>create</b></font> <font color="#6a5acd">procedure</font> testproc() <font color="#6a5acd">begin</font> <font color="#a52a2a"><b>select</b></font> * <font color="#6a5acd">from</font> mytable; <font color="#6a5acd">end</font>;
</pre>
</blockquote>

<p>MySQL 4.1.2 supports native input binds with its Statement API but the Statement API cannot be used to call stored procedures yet. As a result, SQL Relay must fake input binds when calling MySQL stored procedures, so queries which call stored procedures must be written with colon-delimited bind variables rather than MySQL's native ?'s.</p>

<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<pre>
cur-&gt;sendQuery(<font color="#ff00ff">&quot;call testproc()&quot;</font>);
<font color="#2e8b57"><b>char</b></font>    *field00=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *field01=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *field02=cur-&gt;getField(<font color="#ff00ff">0</font>,<font color="#ff00ff">2</font>);
<font color="#2e8b57"><b>char</b></font>    *field10=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">0</font>);
<font color="#2e8b57"><b>char</b></font>    *field11=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">1</font>);
<font color="#2e8b57"><b>char</b></font>    *field12=cur-&gt;getField(<font color="#ff00ff">1</font>,<font color="#ff00ff">2</font>);
...
</pre>
</blockquote>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<pre>
<font color="#a52a2a"><b>drop</b></font> <font color="#6a5acd">procedure</font> testproc
</pre>
</blockquote>




<a name="caching"></a>
<span class="heading2">Caching The Result Set</span><br><br>

<p>Say you're writing a web-based report where a query with a huge result set
is executed and 20 rows are displayed per page.  Rather than rerunning the 
query for every page every time and dumping all but the 20 rows you want to
display, you can run the query once, cache the result set to a local file and
just open the file for each page of the report.</p>

<p>First CGI:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

	... generate a unique filename ...

        cur-&gt;cacheToFile(filename);
        cur-&gt;setCacheTtl(<FONT color=#ff00ff>600</FONT>);
        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;select * from my_table&quot;</FONT>);
        con-&gt;endSession();
        cur-&gt;cacheOff();

        ... pass the filename to the next page ...

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>Second CGI:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>
<font color="#a020f0">#include </font><font color="#f800f8">&lt;iostream.h&gt;</font>

main() {

        ... get the filename from the previous page ...

        ... get the page to display from the previous page ...

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;openCachedResultSet(filename);
        con-&gt;endSession();

        <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> row=pagetodisplay*<FONT color=#ff00ff>20</FONT>; row&lt;(pagetodisplay+<FONT color=#ff00ff>1</FONT>)*<FONT color=#ff00ff>20</FONT>; row++) {
                <B><FONT color=#a52a2a>for</FONT></B> (<B><FONT color=#2e8b57>int</FONT></B> col=<FONT color=#ff00ff>0</FONT>; col&lt;cur-&gt;colCount(); col++) {
                        cout &lt;&lt; cur-&gt;getField(row,col) &lt;&lt; <FONT color=#ff00ff>&quot;,&quot;</FONT>;
                }
                cout &lt;&lt; endl;
        }

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>


<a name="suspending"></a>
<span class="heading2">Suspending and Resuming Sessions</span><br><br>

<p>Sometimes web-based applications need a single database transaction to span
multiple pages.  Since SQL Relay sessions can be suspended and resumed, this
is possible.</p>

<p>First CGI:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;insert into my_table values (1,2,3)&quot;</FONT>);
        cur-&gt;suspendResultSet();
        con-&gt;suspendSession();
        <B><FONT color=#2e8b57>int</FONT></B>     rs=cur-&gt;getResultSetId();
        <B><FONT color=#2e8b57>int</FONT></B>     port=cur-&gt;getConnectionPort();
        <B><FONT color=#2e8b57>char</FONT></B>    *socket=cur-&gt;getConnectionSocket();

        ... pass the rs, port and socket to the next page ...

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>Second CGI:</p>

<blockquote>
<PRE>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;sqlrelay/sqlrclient.h&gt;</FONT>

main() {

        ... get rs, port and socket from previous page ...

        sqlrconnection      *con=<B><FONT color=#a52a2a>new</FONT></B> sqlrconnection(<FONT color=#ff00ff>&quot;host&quot;</FONT>,<FONT color=#ff00ff>9000</FONT>,<FONT color=#ff00ff>&quot;&quot;</FONT>,<FONT color=#ff00ff>&quot;user&quot;</FONT>,<FONT color=#ff00ff>&quot;password&quot;</FONT>,<FONT color=#ff00ff>0</FONT>,<FONT color=#ff00ff>1</FONT>);
        sqlrcursor          *cur=<B><FONT color=#a02828>new</FONT></B> sqlrcursor(con);

        con-&gt;resumeSession(port,socket);
        cur-&gt;resumeResultSet(rs);
        cur-&gt;sendQuery(<FONT color=#ff00ff>&quot;commit&quot;</FONT>);
        con-&gt;endSession();

        <B><FONT color=#a02828>delete</FONT></B> cur;
        <B><FONT color=#a02828>delete</FONT></B> con;
}
</PRE>
</blockquote>

<p>You can also distribute the processing of a result set across a series of
CGI's using suspended sessions.  If you're buffering a result set in chunks
instead of all at once and suspend a session, when you resume the session you 
can continue to retrieve rows from the result set.</p>

<p>Similarly, if you're buffering a result set in chunks, caching that
result set and suspend your session.  When you resume the session, you can
continue caching the result set.  You must use resumeCachedResultSet()
instead of resumeResultSet() however.</p>

<a name="lastinsertid"></a>
<span class="heading2">Getting the Last Insert ID in SQLite</span><br><br>

<p>The SQLite API provides a function which returns the "last insert rowid";
the rowid of the most recent insert into the database or 0 if no inserts have 
ever been performed during the session.</p>

<p>SQL Relay supports fetching the last insert rowid from an SQLite database
as well.  You just have to run the query:

<blockquote>
<b>select last insert rowid</b>
</blockquote>

</body>
</html>
